---@class BattleEntityBar:UIComponentInterface
local BattleEntityBar = Class("BattleEntityBar",UIComponentInterface)
local t = BattleEntityBar

function t:OnCreate(obj,parentView)
    ---@type GComponent
    self.fguiObj = obj
    ---@type Battle.Entity
    self.entityId = nil
    ---@type UIBattleEntityLayer
    self.parent = parentView
    self.hpBar = self:GetChild("Bar_MyHp01");
    self.tempBar = self:GetChild("Bar_MyHp02");
    self.ctrlIsEnemy = self:GetController("isEnemy")
    self.pos = Vector3.New()
    self.visible = true
    self.delayTimeHide = Battle.GlobalConfig.GetInt(28)/1000
    self.hideTime = 0
end
function t:OnDestroy()
    self:DelReferenceAll()
end
---@param entity Battle.Entity
function t:Refresh(entity)
    self.entityId = entity:GetId()
    self.tempBar.value = 100;
    self.hpBar.value = 100;
    self:SetHp(entity:GetHp(),entity:GetHpMax())
    if  entity.camp == Battle.CampType.Blue then
        self.ctrlIsEnemy.selectedIndex = 0;
    else
        self.hpBar = self:GetChild("Bar_EnemyHp01");
        self.tempBar = self:GetChild("Bar_EnemyHp02");
        self.ctrlIsEnemy.selectedIndex = 1;
    end
    local scale = DataTable.GetById("t_hero",entity.modelId,"f_BloodZoom") or 0
    if scale == 0 then
        scale = 1
    else
        scale = scale/100
    end
    self.fguiObj:SetScale(scale,scale)
    self.hideTime = 0
    self:SetVisible(false)
end
function t:SetHp(hp,max)
    self.hideTime = self.delayTimeHide
    if not self.visible then
        self:Update(0)
    end
    self.tempBar:TweenValue(hp/max*100,1.2)
    self.hpBar:TweenValue(hp/max*100,0.3)
end
function t:Update(dt)
    local entity = Battle.ActorManager.GetActor(self.entityId)
    if not entity or (entity.effectStateSystem:IsHide() and entity.camp == Battle.CampType.Red) then
        self:SetVisible(false)
        return
    else
        self:SetVisible(true)
    end
    self.hideTime = self.hideTime - dt
    if self.hideTime <= 0 then
        self:SetVisible(false)
        return
    end
    local pos = entity:GetHPBarPos()
    if self:IsChangePos(pos.x,pos.y,pos.z) then
        Util.WorldToFGUILocal(Battle.SceneMgr.camera,pos.x,pos.y,pos.z,self.fguiObj)
    end
end

function t:IsChangePos(x,y,z)
    return math.abs(self.pos.x - x) > 0.001
            or math.abs(self.pos.y - y ) > 0.001
            or math.abs(self.pos.z - z ) > 0.001
end
function t:SetVisible(visible)
    if self.visible == visible then return end
    self.visible = visible
    self.fguiObj.visible = visible
end
return t
